package com.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.tools.SomeMonitorEditText;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class AddUserActivity extends AppCompatActivity {

    private ImageButton 后退按钮;
    private EditText 编辑用户名;
    private CheckBox 男生;
    private CheckBox 女生;
    private CheckBox 学生;
    private CheckBox 管理员;
    private Button 提交按钮;

    String userName;
    String gender;
    String userCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_user);

        后退按钮 = (ImageButton) findViewById(R.id.后退按钮);
        编辑用户名 = (EditText) findViewById(R.id.编辑用户名);
        男生 = (CheckBox) findViewById(R.id.男生);
        女生 = (CheckBox) findViewById(R.id.女生);
        学生 = (CheckBox) findViewById(R.id.学生);
        管理员 = (CheckBox) findViewById(R.id.管理员);
        提交按钮 = (Button) findViewById(R.id.提交按钮);

        后退按钮.setColorFilter(Color.BLACK);

        后退按钮.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddUserActivity.this.finish();
                Intent intent = new Intent();
                intent.setClass(AddUserActivity.this, SetUserActivity.class);
                startActivity(intent);
            }
        });

        男生.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(男生.isChecked()){
                    女生.setChecked(false);
                }else{
                    女生.setChecked(true);
                }
            }
        });
        女生.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(女生.isChecked()){
                    男生.setChecked(false);
                }else{
                    男生.setChecked(true);
                }
            }
        });

        学生.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(学生.isChecked()){
                    管理员.setChecked(false);
                }else{
                    管理员.setChecked(true);
                }
            }
        });
        管理员.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(管理员.isChecked()){
                    学生.setChecked(false);
                }else{
                    学生.setChecked(true);
                }
            }
        });

        //没写完无法提交
        new SomeMonitorEditText().SetMonitorEditText(提交按钮,编辑用户名);

        提交按钮.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userName = 编辑用户名.getText().toString().trim();
                if(男生.isChecked()){
                    gender="1";
                }else{
                    gender="2";
                }
                if(学生.isChecked()){
                    userCode="2";
                }else {
                    userCode="1";
                }
                String path = "http://101.200.40.47:8080/stu/user/addUser?userName="+userName+
                        "&gender="+gender+
                        "&userCode="+userCode;

                new AddUser().execute(path);
            }
        });

    }

    //编辑课程
    private class  AddUser extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] params) {

            String path = params[0].toString();

            try {
                //以get方式提交整个路径
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");//这个应该都知道是干嘛的，默认是GET  
                conn.setConnectTimeout(5000);

                if (conn.getResponseCode() == 200) {
                    InputStream is = conn.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String str = br.readLine();
                    return str;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            String info = o.toString().trim();
            if(info.equals("1")){
                Toast.makeText(AddUserActivity.this, "添加成功！", Toast.LENGTH_SHORT).show();
                AddUserActivity.this.finish();
                Intent intent = new Intent();
                intent.setClass(AddUserActivity.this, SetUserActivity.class);
                startActivity(intent);
            }else if((info.equals("0"))){
                Toast.makeText(AddUserActivity.this, "用户名已存在！请修改用户名！", Toast.LENGTH_SHORT).show();
                编辑用户名.setText(null);
            }else{
                Toast.makeText(AddUserActivity.this, "未知错误！修改失败！", Toast.LENGTH_SHORT).show();
            }
        }
    }

    long mExitTime;       //实现“再按一次退出”的记录时间变量
    @Override //再按一次退出程序
    public void onBackPressed() {
        if (System.currentTimeMillis() - mExitTime < 2000) {
            super.onBackPressed();
        } else {
            mExitTime = System.currentTimeMillis();
            Toast.makeText(AddUserActivity.this,"再按一次退出应用",Toast.LENGTH_SHORT).show();
        }
    }
}
